<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('model_user');
		$this->load->model('model_job');
	}

	public function index() {
		$this -> login();
	}

	public function login() {
		$this->form_validation->set_rules('email', 'Email', 'valid_email|trim|required|min_length[8]|xss_clean|callback_validate_login');
		$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]|md5');

		if($this->form_validation->run()) {
			$email = $this->input->post('email');
			$id = $this->model_user->get_id($email)['id_dn'];
			$this->session->set_userdata('id',$id);
			if($this->model_user->get_employer_name($id)) {
				// $ten_dn = $this->model_user->get_employer_name($id)['ten_cty'];
				$this->session->set_userdata('ten_dn', $this->model_user->get_employer_name($id)['ten_cty']);
				$this->session->set_userdata('level',1);
			}

			if($this->model_user->get_student_name($id)) {
				// $ten_sv = $this->model_user->get_student_name($id)['ten'];
				$this->session->set_userdata('ten_sv', $this->model_user->get_student_name($id)['ten']);
				$this->session->set_userdata('level',2);

				// $mssv = $this->model_user->get_student_name($id)['mssv'];
				// $test1 = explode("-", $this->model_user->test($mssv)['ki_nang']);
				// $test2 = $test1[1];
			}

			if($this->model_user->get_leader_name($id)){
				$this->session->set_userdata('ten_nv', $this->model_user->get_leader_name($id)['ten']);
				$this->session->set_userdata('level',3);
			}
			$this->session->set_userdata('active', 1);
			$this->session->set_userdata('email', $email);
			// $mssv = $this->model_user->get_student_name($id)['mssv'];
			// $test1 = explode("-", $this->model_user->test($mssv)['ki_nang']);
			// $test2 = $test1[1];

			$url = base_url().'trang-chu';
			header("Location: $url");
			exit();
			echo "thanh cong";
			// $data['home_job'] = $this->model_job->home_job();
			// if(isset($mssv)){
			// 	$data['job_hint'] = $this->model_user->job_hint($test2);
			// }
			// $data['title'] = "Trang Chủ";
			// $data['subview'] = 'users/home';
			// $this->load->view('main', $data);
		}else {
			echo "<script type='text/javascript'> 
						 	 alert('Loi dang nhap hoac tai khoan chua duoc kich hoat!');
							 window.location.href ='".base_url()."trang-chu';
							 </script>";
		}
	}
	public function validate_login() {
		$this->load->model('model_user');
		if($this->model_user->check_login()) {
			return true;
		}else {
			$this->form_validation->set_message('validate_login', 'Incorrect username/password or your account has not been activated');
			return false;
		}
	}
}